﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartSpace.Core;
using Microsoft.Win32;
using System.IO;
using SsTool.Common;

namespace SsTool.Core.Windows
{
    /// <summary>
    /// Interaction logic for ApplicationWindow.xaml
    /// </summary>
    public partial class ApplicationWindow : Window
    {
        private SmartSpace.Core.Application _application;

        public ApplicationWindow()
        {
            InitializeComponent();

            _application = new SmartSpace.Core.Application();
            _application.Id = Guid.NewGuid();
            _application.Name = string.Format("新应用{0}", _application.Id);
            this.DataContext = _application;
        }

        public ApplicationWindow(SmartSpace.Core.Application application)
        {
            InitializeComponent();
            _application = application;

            ICoreService coreService = App.Get<ICoreService>();
            Theme theme = coreService.GetTheme(_application.ThemeId);
            _ctlEditTheme.Text = theme.Name;

            ImageFile image = coreService.GetImage(_application.ImageId);
            if (image != null)
            {
                ShowLogo(image.Data);
            }

            this.DataContext = _application;
        }

        private void ShowLogo(byte[] data)
        {
            if (data != null && data.Length > 0)
            {
                try
                {
                    BitmapImage source = new BitmapImage();
                    source.BeginInit();
                    source.StreamSource = new System.IO.MemoryStream(data);
                    source.EndInit();

                    _ctlImage128.Source = source;
                    _ctlImage64.Source = source;
                    _ctlImage32.Source = source;
                    _ctlImage16.Source = source;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void _ctlBtnLogo_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "图片|*.png";
            if (dlg.ShowDialog() == true)
            {
                byte[] data = null;
                using (Stream stream = dlg.OpenFile())
                {
                    int len = (int)stream.Length;
                    stream.Seek(0, SeekOrigin.Begin);
                    BinaryReader br = new BinaryReader(stream);
                    data = br.ReadBytes((int)stream.Length);
                }

                if (data != null)
                {
                    FileInfo fi = new FileInfo(dlg.FileName);
                    ImageFile image = new ImageFile();
                    image.Id = Guid.NewGuid();
                    image.Extension = fi.Extension;
                    image.SortIndex = 0;
                    image.Data = data;
                    ICoreService service = App.Get<ICoreService>();
                    service.SaveImage(image);
                    _application.ImageId = image.Id;
                    ShowLogo(data);
                }
            }
        }

        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            ICoreService service = App.Get<ICoreService>();
            if (service.SaveApplication(_application))
            {
                DialogResult = true;
            }
            else
            {
                MessageBox.Show("保存失败!");
            }
        }

        private void _ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlBtnSelectTheme_Click(object sender, RoutedEventArgs e)
        {
            SelectThemeWindow window = new SelectThemeWindow();
            if (window.ShowDialog() == true)
            {
                _application.ThemeId = window.SelectedTheme.Id;
                _ctlEditTheme.Text = window.SelectedTheme.Name;
                this.DataContext = null;
                this.DataContext = _application;
            }
        }
    }
}
